package com.css.fxfzysx.modules.lifeline.controller;

import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxVersionEntity;
import com.css.fxfzysx.modules.lifeline.param.RegionParam;
import com.css.fxfzysx.modules.lifeline.service.LifelineService;
import com.css.fxfzysx.modules.lifeline.vo.QueryParamsVo;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformPageUtils;
import com.css.fxfzysx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("lifeline")
@Slf4j
public class LifelineController {

    @Autowired
    LifelineService lifelineService;

    /**
     * 查询重复数据
     */
    @GetMapping("getRepeatData")
    public RestResponse getRepeatData(RegionParam param){
        List<SmxVersionEntity> list = lifelineService.getRepeatData(param);
        return RestResponse.succeed(list);
    }

    /**
     * 上传shp
     * @param param
     * @return
     */
    @PostMapping("/uploadShp")
    public RestResponse uploadShp(RegionParam param) {
        RestResponse response = null;
        try {
            Map<String, Object> map = lifelineService.uploadShp(param);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        } catch (Exception e) {
            String errormessage = "上传失败!";
            response=RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 分页查询
     * @param queryParamsVo
     * @param request
     * @return
     */
    @GetMapping("/page")
    public RestResponse queryFshBuildings(QueryParamsVo queryParamsVo, HttpServletRequest request) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = lifelineService.page(queryParamsVo,curPage,pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("查询成功!");
        }catch (Exception e){
            String errormessage = "查询失败!";
            response=RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 详情页
     * @param id
     * @param type
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id,String type){
        RestResponse restResponse = null;
        try {
            Map map = lifelineService.findById(id, type);
            restResponse = RestResponse.succeed(map.get("data"));
            restResponse.setMessage("查询成功");
        } catch (Exception e) {
            String errormessage = "查询失败";
            restResponse = RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return restResponse;
    }

    /**
     * 桥梁详情页
     * @param id
     * @param flag
     * @return
     */
    @GetMapping("/bridgeFindById")
    public RestResponse bridgeFindById(String id,String flag){
        RestResponse restResponse = null;
        try {
            Map map = lifelineService.bridgeFindById(id, flag);
            restResponse = RestResponse.succeed(map.get("data"));
            restResponse.setMessage("查询成功");
        } catch (Exception e) {
            String errormessage = "查询失败";
            restResponse = RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return restResponse;
    }


    /**
     * 修改
     * @param map
     * @return
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody Map<String,Object>map){
        RestResponse restResponse = null;
        try {
            Boolean res = lifelineService.update(map);
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("修改成功");
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error("修改失败!",e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 删除
     * @param map
     * @return
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody Map<String,Object>map){
        RestResponse restResponse = null;
        try {
            Boolean res = lifelineService.delete(map);
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("删除成功");
        } catch (Exception e) {
            String errormessage = "删除失败";
            e.printStackTrace();
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 桥梁删除
     * @param map
     * @return
     */
    @PostMapping("/bridgeDelete")
    public RestResponse bridgeDelete(@RequestBody Map<String,Object>map){
        RestResponse restResponse = null;
        try {
            Boolean res = lifelineService.bridgeDelete(map);
            restResponse = RestResponse.succeed(res);
            restResponse.setMessage("删除成功");
        } catch (Exception e) {
            String errormessage = "删除失败";
            e.printStackTrace();
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
